package com.yicoollxing.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.yicoollxing.common.ResponseCode;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.dao.mapper.FoodMapper;
import com.yicoollxing.dao.mapper.OrderMapper;
import com.yicoollxing.pojo.Food;
import com.yicoollxing.pojo.Order;
import com.yicoollxing.service.IFoodService;
import com.yicoollxing.service.IOrderService;
import com.yicoollxing.util.DateTimeUtil;
import com.yicoollxing.vo.FoodDetialVo;
import com.yicoollxing.vo.OrderDetialVo;
import com.yicoollxing.vo.OrderListVo;

@Service("iOrderService")
public class OrderServiceImpl implements IOrderService {

	@Autowired
	private OrderMapper orderMapper;

	@Autowired
	private FoodMapper foodMapper;

	@Autowired
	private IFoodService iFoodService;

	public ServerResponse list(Integer pageNum,Integer pageSize,Integer mid) {
		if (mid == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}

		List<Order> orderList = orderMapper.selectByMid(mid);
		List<OrderListVo> orderListVoList = Lists.newArrayList();
		for (Order order : orderList) {
			orderListVoList.add(assembleOrderList(order));
		}
		PageHelper pageHelper = new PageHelper();
		pageHelper.startPage(pageNum, pageSize);
		PageInfo pageInfo = new PageInfo(orderListVoList);

		return ServerResponse.createBySuccess(pageInfo);
	}

	private OrderListVo assembleOrderList(Order order) {
		OrderListVo orderListVo = new OrderListVo();
		orderListVo.setId(order.getId());
		orderListVo.setOrderNo(order.getOrderNo());
		orderListVo.setOrderMid(order.getOrderMid());
		orderListVo.setUserId(order.getUserId());
		orderListVo.setPayStatus(order.getPayStatus());
		orderListVo.setTotle(order.getTotle());
		orderListVo.setOrderMethod(order.getOrderMethod());
		orderListVo.setCreateTime(new DateTimeUtil().dateToStr(order
				.getCreateTime()));
		orderListVo.setUpdateTime(new DateTimeUtil().dateToStr(order
				.getUpdateTime()));
		String foodIds = order.getFoodids();
		String[] foodIdArray = null;
		List<FoodDetialVo> foodDetailVoList = null;
		if (StringUtils.isNotBlank(foodIds)) {
			foodDetailVoList = Lists.newArrayList();
			foodIdArray = foodIds.split(",");
			for (int i = 0; i < foodIdArray.length; i++) {
				if (StringUtils.isNotBlank(foodIdArray[i])) {
					FoodDetialVo foodDetialVo = (FoodDetialVo) iFoodService
							.getFoodDetail(Integer.valueOf(foodIdArray[i]))
							.getData();
					foodDetailVoList.add(foodDetialVo);
				}
			}
			orderListVo.setFoodDetialVoList(foodDetailVoList);
		}
		return orderListVo;
	}

	public ServerResponse detail(Integer orderId) {
		if (orderId == null) {
			return ServerResponse.createByErrorCodeMessage(
					ResponseCode.ILLEGAL_ARGUMENT.getCode(),
					ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		Order order = orderMapper.selectByPrimaryKey(orderId);
		if(order==null){
			return ServerResponse.createByErrorMessage("订单不存在");
		}
		OrderDetialVo orderDetialVo = assembleOrderDetailVo(order);

		return ServerResponse.createBySuccess(orderDetialVo);
	}

	private OrderDetialVo assembleOrderDetailVo(Order order) {
		OrderDetialVo orderDetialVo = new OrderDetialVo();
		orderDetialVo.setId(order.getId());
		orderDetialVo.setOrderNo(order.getOrderNo());
		orderDetialVo.setOrderMethod(order.getOrderMethod());
		orderDetialVo.setOrderMid(order.getOrderMid());
		orderDetialVo.setOrderDid(order.getOrderDid());
		orderDetialVo.setStatus(order.getStatus());
		orderDetialVo.setUserId(order.getUserId());
		orderDetialVo.setActivatecode(order.getActivatecode());
		orderDetialVo.setPhone(order.getPhone());
		orderDetialVo.setTotle(order.getTotle());
		orderDetialVo.setDiscount(order.getDiscount());
		orderDetialVo.setPaymethod(order.getPaymethod());
		orderDetialVo.setPayStatus(order.getPayStatus());
		orderDetialVo.setVoucherid(order.getVoucherid());
		orderDetialVo.setNote(order.getNote());
		orderDetialVo.setCreateTime(new DateTimeUtil().dateToStr(order
				.getCreateTime()));
		orderDetialVo.setUpdateTime(new DateTimeUtil().dateToStr(order
				.getUpdateTime()));
		
		String foodIds = order.getFoodids();
		String[] foodIdArray = null;
		List<FoodDetialVo> foodDetailVoList = null;
		if (StringUtils.isNotBlank(foodIds)) {
			foodDetailVoList = Lists.newArrayList();
			foodIdArray = foodIds.split(",");
			for (int i = 0; i < foodIdArray.length; i++) {
				if (StringUtils.isNotBlank(foodIdArray[i])) {
					FoodDetialVo foodDetialVo = (FoodDetialVo) iFoodService
							.getFoodDetail(Integer.valueOf(foodIdArray[i]))
							.getData();
					foodDetailVoList.add(foodDetialVo);
				}
			}
			orderDetialVo.setFoodDetialVoList(foodDetailVoList);
		}
		return orderDetialVo;
	}

	public ServerResponse serach(Integer orderNo){
		if(orderNo==null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
		}
		Order order=orderMapper.selectByOrderNo(orderNo);
		if(order==null){
			return ServerResponse.createByErrorMessage("订单不存在");
		}
       	OrderListVo orderListVo=assembleOrderList(order);
		return ServerResponse.createBySuccess(orderListVo);
	}
}
